/*
 * Copyright (C) 拾光分享网
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 * Version：V1.0.2(20230701).
 */
package top.hiai.index;

import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import top.hiai.common.constant.StstemDataConstants;
import top.hiai.common.group.SystemDataService;
import top.hiai.common.model.Article;
import top.hiai.common.model.User;
import top.hiai.common.util.RestTime;
import top.hiai.common.util.SensitiveWordsKit;
import top.hiai.config.BaseController;
import top.hiai.my.favorite.MyFavoriteService;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author www.hiai.top
 * @Email goodsking@163.com
 * @Message 更多资源尽在www.hiai.top
 * @Introduction 类的介绍
 */

public class IndexController extends BaseController {
    IndexService indexService = new IndexService();
    SystemDataService systemDataService = new SystemDataService();
    private Map<String, Integer> map = new HashMap<>();

    /**
     * 渲染网站首页页面
     */
    public void index() {
        //获取用户列表
        setAttr("articleList", indexService.paginate(getParaToInt("p", 1), limtCount, 0));
        //获取热门文章
        findHotArticle();
        /** 广告位置*/
        setAttr("advImg", systemDataService.findByName(StstemDataConstants.INDEX_ADV));
        setAttr("homeAdv", systemDataService.findByName(StstemDataConstants.HOME_ADV));
        render("index.html");
    }

    public void download() {
        //获取用户列表
        setAttr("articleList", indexService.paginate(getParaToInt("p", 1), limtCount, 1));
        //获取热门文章
        findHotArticle();
        /** 广告位置*/
        setAttr("advImg", systemDataService.findByName(StstemDataConstants.DOWNLOAD_ADV));
        render("download.html");
    }

    /**
     * 渲染网站文章详情页面
     */
    public void detail() {
        //获取ID
        Integer aid = getParaToInt();
        //在数据库中查找
        Article article = indexService.getDetailArticle(aid);
        if (article == null || article.getIs_del() == 1) {
            System.out.println(11);
            render("no_status.html");
//            render("../error/404.html");
        } else {
            //传标题
            setAttr("seoTitle", article.get("title"));
            //前台传值
            setAttr("article", article);
            /** 广告位置*/
            setAttr("advImg", systemDataService.findByName(StstemDataConstants.ARTICLE_ADV));
            setAttr("articleHot", indexService.getArticleHotList());
            //获取用户评论
            setAttr("replyList", indexService.replyPaginate(getParaToInt("p", 1), limtCount, aid));

            setLikeAndFavoriteStatus(article);

            /**
             * 判断用户是否重复提交数据，重复请求判断
             *
             * 调用幂等性工具判断
             *
             * */
//        if (IdempotentUtil.check(String.valueOf(aid), this.getClass())) {
//
//        }
            //文章浏览量加1
            indexService.initArticleView(aid);
            render("detail.html");
        }
    }

    /**
     * 遍历热门话题，热门用户
     */
    public void findHotArticle() {
        //热门文章
        setAttr("articleHot", indexService.getArticleHotList());
        //热门分类
        setAttr("commentsHot", indexService.getCommentsHotList());
        //热门用户
        setAttr("userHot", indexService.getUserHotList());
    }

    /**
     * 如果用户已登录，则需要显示当前 article 是否已经被该用户点赞、收藏了
     */
    private void setLikeAndFavoriteStatus(Article article) {
        Ret ret = Ret.create();
        MyFavoriteService.me.setFavoriteStatus(getLoginAccount(), "article", article, ret);
        setAttr("ret", ret);
    }

    /**
     * 文章评论功能
     */
    public void saveArticleReply() {
        if (notLogin()) {
            renderJson(Ret.fail().set("msg", "登录后才可以评论"));
            return;
        }
        //休息时间不能发表文章评论
        String restTimeMsg = RestTime.checkRestTime(getLoginAccount());
        if (restTimeMsg != null) {
            renderJson(Ret.fail().set("msg", restTimeMsg));
            return;
        }
        String replyContent = getPara("replyContent");
        if (StrKit.isBlank(replyContent)) {
            renderJson(Ret.fail().set("msg", "回复内容不能为空"));
            return;
        }

        if (SensitiveWordsKit.checkSensitiveWord(replyContent) != null) {
            renderJson(Ret.fail().set("msg", "回复内容不能包含敏感词"));
            return;
        }

        Ret ret = indexService.saveArticleReply(getParaToInt("articleId"), getLoginAccountId(), replyContent);

        // 注入 nickName 与 avatar 便于 renderToString 生成 replyItem html 片段
        User loginAccount = getLoginAccount();
        ret.set("loginAccount", loginAccount);
        // 用模板引擎生成 HTML 片段 replyItem
        String replyItem = renderToString("_reply_item.html", ret);

        ret.set("replyItem", replyItem);
        renderJson(ret);
    }
}